package com.inctech.controller.account;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inctech.service.IPassportService;

@Controller
@RequestMapping("/account")
public class AccountController {
	@Resource(name = "passportService")
	IPassportService passportService;
	 
	@RequestMapping("/login")
	public String login() {
		System.out.println("/acount/login is handled by action ");
		return "account/login";
	}
	
	@ResponseBody
	@RequestMapping(value = "/loginpass", method = {RequestMethod.POST})
	public String loginpass(HttpServletRequest rquest, HttpServletResponse response) {
		Map<String, String> param = new HashMap<String, String>();
    	String usr = rquest.getParameter("usr");
    	String pwd = rquest.getParameter("pwd");
    	HttpSession session = rquest.getSession();
    	if(usr != null)
    		usr = usr.trim();
    	if(pwd != null)
    		pwd = pwd.trim();
    	param.put("usr", usr);
    	param.put("pwd", pwd);
    	if(validateParam(param)) {
    		session.setAttribute("currentUser", usr);
    		return "{\"result\":\"success\"}";
    	}
    	else {
    		session.setAttribute("currentUser", null);
    		return "{\"result\":\"failure\"}";
    	}	
	}
	
	//Android login action
	@ResponseBody
	@RequestMapping(value = "/androidLoginpass", method = {RequestMethod.POST})
	public String androidLoginpass(String usr, String pwd, HttpServletRequest request) {
		HttpSession session = request.getSession();
		Map<String, String> param = new HashMap<String, String>();
    	if(usr != null)
    		usr = usr.trim();
    	if(pwd != null)
    		pwd = pwd.trim();
    	param.put("usr", usr);
    	param.put("pwd", pwd);
    	JSONObject resultObj = new JSONObject();
    	
    	if(validateParam(param)) {
    		session.setAttribute("currentUser", usr);
    		resultObj.put("status", "successful");
    	}
    	else {
    		session.setAttribute("currentUser", null);
    		resultObj.put("status", "failed");
    	}	
	   	System.out.println("result json");
    	System.out.println(resultObj.toString());
    	resultObj.put("sessionId", session.getId());
    	resultObj.put("currentUser", usr);
		return resultObj.toString();
	}
	
	//Android get logined user name
	@ResponseBody
	@RequestMapping("/androidGetUsr")
	public String androidGetUsr(HttpServletRequest request) {
		HttpSession session = request.getSession();
		JSONObject resultObj = new JSONObject();
		Object usrObj = session.getAttribute("currentUser");
		resultObj.put("sessionId", session.getId());
		String usr = "";
		if(usrObj != null)
			usr = usrObj.toString();
		resultObj.put("currentUser", usr);
		System.out.println("get usr");
		System.out.println(resultObj.toString());
		return resultObj.toString();
	}
	
	private boolean validateParam(Map<String, String> param) {
		String usr = (param.get("usr") != null) ? param.get("usr").toString() : "";
		String pwd = (param.get("pwd") != null) ? param.get("usr").toString() : "";
    	if(usr != null && pwd != null && usr != "" && pwd != "" && passportService.securityCheck(param))
    		return true;
    	else
    		return false;
    }
}
